#include "threadPool.h"
#define MAX_EVENTS 2
#define BUFFER_SIZE 4096
#include <ctype.h>
char **split_words(char *str, int *count)
{
    int i, j, k;
    char **words = NULL;
    char *word = NULL;
    *count = 0;
    i = j = 0;
    while (str[i])
    {
        while (isspace(str[i]))
            i++; // skip leading spaces
        j = i;
        while (!isspace(str[j]) && str[j])
            j++; // find end of word
        if (j > i)
        {                                              // if word found
            word = malloc(sizeof(char) * (j - i + 1)); // allocate memory for word
            if (!word)
            { // check if allocation failed
                for (k = 0; k < *count; k++)
                    free(words[k]); // free memory allocated for previous words
                free(words);        // free memory allocated for word pointers
                *count = 0;         // reset count
                return NULL;        // return NULL
            }
            strncpy(word, str + i, j - i);                     // copy word
            word[j - i] = '\0';                                // add null terminator
            (*count)++;                                        // increment count
            words = realloc(words, sizeof(char *) * (*count)); // allocate memory for new word pointer
            words[*count - 1] = word;                          // add word pointer to array
        }
        i = j + 1; // move to next word
    }
    return words;
}
int exitPipe[2];
void sigFunc(int signum)
{
    printf("signum = %d\n", signum);
    write(exitPipe[1], "1", 1);
    printf("parent got signal\n");
}

int main(int argc, char *argv[])
{
    // ./client 192.168.253.128 1234
    ARGS_CHECK(argc, 3);
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(atoi(argv[2]));
    serverAddr.sin_addr.s_addr = inet_addr(argv[1]);

    // argv[1] "192.168.118.128" --> 32bit的大端ip地址
    usraddr usr;
    int ret = connect(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
    ERROR_CHECK(ret, -1, "connect");
    //循环输入用户名和密码，直到输入正确
    while (1)
    {
        // 输入用户名，密码
        
        memset(&usr, 0, sizeof(usr));
        printf("Please input usrAddress :");
        scanf("%s", usr.usrAddr);
        printf("Enter password :");
        scanf("%s", usr.pwd);

        // 发送用户名
        usr.dataLen = strlen(usr.usrAddr);
        send(sockfd, &usr.dataLen, sizeof(int), 0);
        sendn(sockfd, usr.usrAddr, usr.dataLen);
        // 发送密码
        usr.dataLen = strlen(usr.pwd);
        send(sockfd, &usr.dataLen, sizeof(int), 0);
        sendn(sockfd, usr.pwd, usr.dataLen);
        // 接收验证结果，根据验证结果来决定是否要继续登录
        recv(sockfd, &usr.flag, sizeof(int), 0);
        if (usr.flag == 1)
        {
            break;
        }
    }
    

    // 启动epoll 监听sockfd
    int epfd = epollCtor();
    char buf[BUFFER_SIZE];
    char recvBuf[BUF_SIZE], sendBuf[BUF_SIZE];
    int recvLen;
    while (1)
    {
        while (bzero(buf, sizeof(buf)), read(STDIN_FILENO, buf, sizeof(buf)) > 0)
        {

            int count, i;
            char **words = split_words(buf, &count);
            int sendLen = strlen(buf);
            send(sockfd, &sendLen, sizeof(int), 0); // 发命令给服务器端
            sendn(sockfd, buf, sendLen);

            if (strcmp(words[0], "gets") == 0)
            {
                system("clear");
                printf("start recvFile\n");
                recvFile(sockfd);
                printf("recvFile finished\n");
            }
            if (strcmp(words[0], "puts") == 0)
            {
                system("clear");
                printf("start transFile\n");
                transFile(sockfd, words[1]);
                printf("transFile finished\n");
            }
            if (1 == count)
            {
                if (strcmp(words[0], "pwd") == 0)
                {
                    system("clear");
                    recv(sockfd, &recvLen, sizeof(int), 0); // 接cd之后的路径
                    recvn(sockfd, recvBuf, recvLen);
                    printf("%s\n", recvBuf);
                }
            }
            if (2 == count)
            {
                if (strcmp(words[0], "cd") == 0)
                    system("clear");
                recv(sockfd, &recvLen, sizeof(int), 0); // 接cd之后的路径
                recvn(sockfd, recvBuf, recvLen);
                printf("%s\n", recvBuf);
                {
                }

                if (strcmp(words[0], "ls") == 0)
                {
                    system("clear");
                    while (1)
                    {
                        recv(sockfd, &recvLen, sizeof(int), 0); // 接文件信息
                        if (recvLen == 0)
                        {
                            break;
                        }
                        recvn(sockfd, recvBuf, recvLen);
                        printf("%s\n", recvBuf);
                    }
                }
                if (strcmp(words[0], "remove") == 0)
                {
                    system("clear");
                    recv(sockfd, &recvLen, sizeof(int), 0); // 接cd之后的路径
                    recvn(sockfd, recvBuf, recvLen);
                    printf("%s\n", recvBuf);
                }
            }
        }
    }
}